﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;
using DTC.Database;

namespace DTC.Converters
{
    [ValueConversion(typeof(DTCDrugUsePeriod), typeof(string))]
    class FreqOfUsePeriodToString : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
            CultureInfo culture)
        {
            return Convert(value);
        }
        public object ConvertBack(object value, Type targetType, object parameter, 
            CultureInfo cultrure)
        {
            return ConvertBack(value);
        }

        public object Convert(object value)
        {
            if(!(value is DTCDrugUsePeriod))
                return "";
            if(value == null)
                return "";
            DTCDrugUsePeriod dup = (DTCDrugUsePeriod) value;
            int durationHours = 0;
            if(dup.DurationHours.HasValue)
                durationHours = dup.DurationHours.Value;
            double periodLength = 0;
            if (dup.Period.HasValue)
                periodLength = dup.Period.Value;
            int count = 0;
            if (periodLength != 0)
                count  = durationHours/(int)periodLength;
            string UseCount= string.Empty;
            string UseCountSign = string.Empty;
            string Multiplier = string.Empty;
            string Period = string.Empty;
            string PeriodLenght = string.Empty;

            if(!(count == 1))
                UseCount = count.ToString()+" ";
            if (count >= 2 && count <= 4)
                UseCountSign = "раза";
            else UseCountSign = "раз";
            Multiplier = "в";
            periodLength *= count;
            periodLength = periodLength / 24;
            if (periodLength < 7)
            {
                if (periodLength % 10 == 1)
                {
                    PeriodLenght = "день";
                }
                else if (periodLength % 10 >= 2 && periodLength % 10 <= 4)
                    PeriodLenght = "дня";
                else PeriodLenght = "дней";
            }
            else if (periodLength >= 7 && periodLength <= 28)
            {
                periodLength = Math.Floor(periodLength / 7);
                if (periodLength % 10 == 1)
                {
                    PeriodLenght = "неделю";
                }
                else if (periodLength % 10 >= 2 && periodLength % 10 <= 4)
                {
                    PeriodLenght = "недели";
                }
                else PeriodLenght = "недель";
            }
            else
            {
                periodLength = Math.Floor(periodLength / 30);
                if (periodLength % 10 == 1)
                {
                    PeriodLenght = "месяц";
                }
                else if (periodLength % 10 >= 2 && periodLength % 10 <= 4)
                    PeriodLenght = "месяца";
                else PeriodLenght = "месяцев";
            }
            if(!(periodLength == 1))
                Period = periodLength.ToString()+" ";
            return UseCount + UseCountSign + " " + Multiplier + " "
                    + Period + PeriodLenght;
        }
        public object ConvertBack(object value)
        {
            if (value == null)
                return null;
            string[] substring = ((string)value).Split(new char[] { ' ' });
            if (substring.Length < 3)
                return null;
            string tmpstr;
            double UseCount = 1;
            double PeriodLength = 1;
            double PeriodLengthMultiplier = 1;

            for (int i = 0; i < substring.Length; i++)
            {
                substring[i] = substring[i].Trim();
                if (substring[i].StartsWith("раз"))
                {
                    if (i == 1)
                        double.TryParse(substring[0], out UseCount);
                }
                if (substring[i] == "в")
                {
                    if (i + 1 < substring.Length)
                    {
                        if (!double.TryParse(substring[i + 1], out PeriodLength))
                        {
                            PeriodLength = 1;
                        }
                    }
                    else throw new InvalidCastException();
                }
            }
            if (substring[substring.Length - 1].StartsWith("нед"))
                PeriodLengthMultiplier = 7;
            else if (substring[substring.Length - 1].StartsWith("мес"))
                PeriodLengthMultiplier = 30;
            DTCDrugUsePeriod newPeriod = new DTCDrugUsePeriod()
            {
                DurationHours =(int)(PeriodLengthMultiplier * PeriodLength * 24),
                Period = PeriodLengthMultiplier * PeriodLength * 24 / UseCount
            };
            return newPeriod;
        }

    }
}
